package doc2db.stdstreams;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.StyledDocument;

/**
 *
 * @author yura
 */
public class Out extends PrintStream {

    private int end = 0;
    private JTextPane text;
    private MutableAttributeSet attribute;

    public Out(String file, JTextPane t, MutableAttributeSet color) throws FileNotFoundException{
        this(file);
        this.text = t;
        this.attribute = color;
    }

    public Out(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
        super(file, csn);
    }

    public Out(File file) throws FileNotFoundException {
        super(file);
    }

    public Out(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
        super(fileName, csn);
    }

    public Out(String fileName) throws FileNotFoundException {
        super(fileName);
    }

    public Out(OutputStream out, boolean autoFlush, String encoding)
            throws UnsupportedEncodingException {
        super(out, autoFlush, encoding);
    }

    public Out(OutputStream out, boolean autoFlush) {
        super(out, autoFlush);
    }

    public Out(OutputStream out) {
        super(out);
    }

    @Override
    public PrintStream append(CharSequence csq) {
        print(csq.toString());
        return this;
    }

    @Override
    public PrintStream append(CharSequence csq, int start, int end) {
        print(csq.subSequence(start, end).toString());
        return this;
    }

    @Override
    public PrintStream append(char c) {
        print("" + c);
        return this;
    }

    @Override
    public boolean checkError() {
        return false;
    }

    @Override
    protected void clearError() {
    }

    @Override
    public void close() {
    }

    @Override
    public void flush() {
    }

    @Override
    public PrintStream format(String format, Object... args) {
        print(String.format(format, args));
        return this;
    }

    @Override
    public PrintStream format(Locale l, String format, Object... args) {
        print(String.format(l, format, args));
        return this;
    }

    @Override
    public void print(boolean b) {
        print("" + b);
    }

    @Override
    public void print(char c) {
        print("" + c);
    }

    @Override
    public void print(int i) {
        print("" + i);
    }

    @Override
    public void print(long l) {
        print("" + l);
    }

    @Override
    public void print(float f) {
        print("" + f);
    }

    @Override
    public void print(double d) {
        print("" + d);
    }

    @Override
    public void print(char[] s) {
        print(new String(s));
    }

    @Override
    public void print(String s) {
        //text.insert("" + s, text.getText().length() - this.end);
        try {
            StyledDocument doc = text.getStyledDocument();
            doc.insertString(doc.getLength(), s, attribute);
        } catch (BadLocationException ex) {
            //JOptionPane.showMessageDialog(null, ex);
            MyOut.stderr.println(getClass().getCanonicalName() + "<=>"
                    + MyOut.class.getCanonicalName() + ": "
                    + ex.getLocalizedMessage());
        }
    }

    @Override
    public void print(Object obj) {
        print(obj.toString());
    }

    @Override
    public PrintStream printf(String format, Object... args) {
        return format(format, args);
    }

    @Override
    public PrintStream printf(Locale l, String format, Object... args) {
        return format(l, format, args);
    }

    @Override
    public void println() {
        println("");
    }

    @Override
    public void println(boolean x) {
        println("" + x);
    }

    @Override
    public void println(char x) {
        println("" + x);
    }

    @Override
    public void println(int x) {
        println("" + x);
    }

    @Override
    public void println(long x) {
        println("" + x);
    }

    @Override
    public void println(float x) {
        println("" + x);
    }

    @Override
    public void println(double x) {
        println("" + x);
    }

    @Override
    public void println(char[] x) {
        println(new String(x));
    }

    @Override
    public void println(String x) {
        print(x + "\n");
    }

    @Override
    public void println(Object x) {
        println(x.toString());
    }

    @Override
    protected void setError() {
        super.setError();
        //text.setForeground(Color.red);
    }

    @Override
    public void write(int b) {
        print("" + ((char)b));
    }

    @Override
    public void write(byte[] buf, int off, int len) {
        print(new String(buf, off, len));
    }
}
